package com.geen.tuoke.utils.log;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ZipUtils {
    private static final int BUFFER = 8192;

    public ZipUtils() {
    }

    public static void compress(String srcPath, String dstPath) throws IOException {
        File srcFile = new File(srcPath);
        File dstFile = new File(dstPath);
        if (!srcFile.exists()) {
            throw new FileNotFoundException(srcPath + "不存在！");
        } else {
            FileOutputStream out = null;
            ZipOutputStream zipOut = null;

            try {
                out = new FileOutputStream(dstFile);
                CheckedOutputStream cos = new CheckedOutputStream(out, new CRC32());
                zipOut = new ZipOutputStream(cos);
                String baseDir = "";
                compress(srcFile, zipOut, baseDir);
            } finally {
                if (null != zipOut) {
                    zipOut.close();
                    out = null;
                }

                if (null != out) {
                    out.close();
                }

            }

        }
    }

    private static void compress(File file, ZipOutputStream zipOut, String baseDir) throws IOException {
        if (file.isDirectory()) {
            compressDirectory(file, zipOut, baseDir);
        } else {
            compressFile(file, zipOut, baseDir);
        }

    }

    private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir) throws IOException {
        File[] files = dir.listFiles();

        for (int i = 0; i < files.length; ++i) {
            compress(files[i], zipOut, baseDir + dir.getName() + "/");
        }

    }

    private static void compressFile(File file, ZipOutputStream zipOut, String baseDir) throws IOException {
        if (file.exists()) {
            BufferedInputStream bis = null;

            try {
                bis = new BufferedInputStream(new FileInputStream(file));
                ZipEntry entry = new ZipEntry(baseDir + file.getName());
                zipOut.putNextEntry(entry);
                byte[] data = new byte[8192];

                int count;
                while ((count = bis.read(data, 0, 8192)) != -1) {
                    zipOut.write(data, 0, count);
                }
            } finally {
                if (null != bis) {
                    bis.close();
                }
            }
        }
    }

    public static void decompress(String zipFile, String dstPath) throws IOException {
        File pathFile = new File(dstPath);
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }

        ZipFile zip = new ZipFile(zipFile);
        Enumeration entries = zip.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            String zipEntryName = entry.getName();
            InputStream in = null;
            FileOutputStream out = null;

            try {
                in = zip.getInputStream(entry);
                String outPath = (dstPath + "/" + zipEntryName).replaceAll("\\*", "/");
                File file = new File(outPath.substring(0, outPath.lastIndexOf(47)));
                if (!file.exists()) {
                    file.mkdirs();
                }

                if (!(new File(outPath)).isDirectory()) {
                    out = new FileOutputStream(outPath);
                    byte[] buf1 = new byte[1024];

                    int len;
                    while ((len = in.read(buf1)) > 0) {
                        out.write(buf1, 0, len);
                    }
                }
            } finally {
                if (null != in) {
                    in.close();
                }

                if (null != out) {
                    out.close();
                }

            }
        }

    }
}
